package edu.uoc.tdp.pac3.client;

import javax.swing.*;
import java.util.*;
import java.rmi.RemoteException;
import edu.uoc.tdp.pac3.common.tools.*;
import edu.uoc.tdp.pac3.common.GestorIncidenciesInterface;
import edu.uoc.tdp.pac3.beans.Actuacio;


/**
 * Clase Pantalla per veure els HIstorics
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class PantallaHistoric extends JPanel {

    private GestorIncidenciesInterface objServeis = null;
    private TDSLanguageUtils mess = null;
    private JLabel lblTitle = null;
    private JLabel lblIncidencia = null;
    private JLabel lblResol = null;
    private JTextArea txtArea = null;
    private JComboBox cmbIncidencia = null;
    private JComboBox cmbSolved = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
    }

    /**
     * This is the default constructor
     */
    public PantallaHistoric() {
        super();
        initialize();
    }

    public PantallaHistoric(GestorIncidenciesInterface pObjServeis) throws RemoteException {
        super();
        objServeis = pObjServeis;
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        lblTitle = new JLabel();
        lblTitle.setBounds(20, 25, 300, 20);
        lblTitle.setText(mess.getMessage("history.title"));
        lblTitle.setHorizontalTextPosition(SwingConstants.LEFT);

        lblResol = new JLabel();
        lblResol.setBounds(20, 60, 250, 20);
        lblResol.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblResol.setText(mess.getMessage("history.label.solved"));

        lblIncidencia = new JLabel();
        lblIncidencia.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblIncidencia.setBounds(20, 90, 250, 20);
        lblIncidencia.setText(mess.getMessage("history.label.request"));

        txtArea = new JTextArea();
        txtArea.setBorder(javax.swing.BorderFactory.createTitledBorder(mess.getMessage("history.area.title")));
        txtArea.setAutoscrolls(true);
        txtArea.setBounds(10, 120, 655, 225);
        txtArea.setEditable(false);

        this.setLayout(null);
        this.setBounds(0, 0, 800, 400);
        this.add(lblTitle, null);

        this.add(lblResol, null);
        this.add(getCmbSolved(), null);

        this.add(lblIncidencia, null);
        this.add(getCmbIncidencia(), null);

        this.add(txtArea);

    }

    private JComboBox getCmbSolved() {

        cmbSolved = new JComboBox();
        ComboItem cmbItem = new ComboItem(0, mess.getMessage("history.combo.solved.no"));
        cmbSolved.addItem(cmbItem);
        cmbItem = new ComboItem(1, mess.getMessage("history.combo.solved.yes"));
        cmbSolved.addItem(cmbItem);
        cmbSolved.setBounds(300, 60, 370, 25);
        cmbSolved.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent e) {
                Boolean opt = false;
                ComboItem opcio = (ComboItem) cmbSolved.getSelectedItem();
                if (opcio.getID() == 1) {
                    opt = true;
                }
                try {
                    cmbIncidencia.removeAllItems();
                    for (Iterator<ComboItem> it = objServeis.vctIncidencia(opt).iterator(); it.hasNext();) {
                        cmbIncidencia.addItem(it.next());
                    }
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null,
                            mess.getMessage("history.err.rmi") + "\n" + e1.getMessage(),
                            mess.getMessage("history.err"), JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        return cmbSolved;
    }

    private JComboBox getCmbIncidencia() {
        if (cmbIncidencia == null) {
            try {
                cmbIncidencia = new JComboBox(objServeis.vctIncidencia(false));
                cmbIncidencia.setBounds(300, 90, 370, 25);
                cmbIncidencia.addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        if (cmbIncidencia.getSelectedItem() != null) {
                            ComboItem opcio = (ComboItem) cmbIncidencia.getSelectedItem();
                            txtArea.setText("");
                            try {
                                Iterator<Actuacio> it = objServeis.lstNoteList(opcio.getID()).iterator();                                
                                while(it.hasNext()) {
                                    Actuacio act = (Actuacio) it.next();
                                    txtArea.append(act.getCreationDate().toString()+ "\n");                                    
                                    txtArea.append(act.getDescription() + "\n\n");
                                }
                            } catch (Exception e1) {
                                JOptionPane.showMessageDialog(null,
                                        mess.getMessage("history.err.rmi") + "\n" + e1.getMessage(),
                                        mess.getMessage("history.err"), JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    }
                });



            } catch (RemoteException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("history.err.rmi") + "\n" + e.getMessage(),
                        mess.getMessage("history.err"), JOptionPane.ERROR_MESSAGE);
            }
        }
        return cmbIncidencia;
    }
}
